package me.gavincook.mybatis.plugins;

import java.util.List;

import org.mybatis.generator.api.CommentGenerator;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

/**
 * 查询条件增强，将条件addCriterion(String condition, Object value, String property)方法由protected修改为public
 *
 * @author gavincook
 * @version $ID: PaginationPlugin.java, v0.1 2018-01-08 11:48 gavincook Exp $$
 */
public class ExampleCriteriaEnhancePlugin extends PluginAdapter {


    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        List<InnerClass> innerClasses = topLevelClass.getInnerClasses();
        if(innerClasses != null){
            for(InnerClass innerClass : innerClasses){
                if("GeneratedCriteria".equals(innerClass.getType().getShortName())){
                    for(Method method : innerClass.getMethods()){
                        //只处理addCriterion(String condition, Object value, String property)方法
                        if("addCriterion".equals(method.getName()) && method.getParameters().size() == 3){
                            method.setVisibility(JavaVisibility.PUBLIC);
                            break;
                        }
                    }
                    break;
                }
            }
        }
        return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
    }

}
